6c9972c8deb6d63c66a39db949add47f815725ab
3 * Classes used to send e-mails
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author <brion@pobox.com>
22 * @author <mail@tgries.de>
23 * @author Tim Starling
24 * @author Luke Welling lwelling@wikimedia.org
28 * This module processes the email notifications when the current page is
29 * changed. It looks up the table watchlist to find out which users are watching
32 * The current implementation sends independent emails to each watching user for
33 * the following reason:
35 * - Each watching user will be notified about the page edit time expressed in
36 * his/her local time (UTC is shown additionally). To achieve this, we need to
37 * find the individual timeoffset of each watching user from the preferences..
39 * Suggested improvement to slack down the number of sent emails: We could think
40 * of sending out bulk mails (bcc:user1,user2...) for all these users having the
41 * same timeoffset in their preferences.
43 * Visit the documentation pages under http://meta.wikipedia.com/Enotif
45 class EmailNotification
{
46 protected $subject, $body, $replyto, $from;
47 protected $timestamp, $summary, $minorEdit, $oldid, $composed_common, $pageStatus;
48 protected $mailTargets = array();
61 * @param User $editor The editor that triggered the update. Their notification
62 * timestamp will not be updated(they have already seen it)
63 * @param Title $title The title to update timestamps for
64 * @param string $timestamp Set the update timestamp to this value
67 public static function updateWatchlistTimestamp( User
$editor, Title
$title, $timestamp ) {
68 global $wgEnotifWatchlist, $wgShowUpdatedMarker;
70 if ( !$wgEnotifWatchlist && !$wgShowUpdatedMarker ) {
74 $dbw = wfGetDB( DB_MASTER
);
75 $res = $dbw->select( array( 'watchlist' ),
78 'wl_user != ' . intval( $editor->getID() ),
79 'wl_namespace' => $title->getNamespace(),
80 'wl_title' => $title->getDBkey(),
81 'wl_notificationtimestamp IS NULL',
86 foreach ( $res as $row ) {
87 $watchers[] = intval( $row->wl_user
);
91 // Update wl_notificationtimestamp for all watching users except the editor
93 $dbw->onTransactionIdle(
94 function () use ( $dbw, $timestamp, $watchers, $title, $fname ) {
95 $dbw->update( 'watchlist',
97 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
99 'wl_user' => $watchers,
100 'wl_namespace' => $title->getNamespace(),
101 'wl_title' => $title->getDBkey(),
112 * Send emails corresponding to the user $editor editing the page $title.
114 * May be deferred via the job queue.
116 * @param User $editor
117 * @param Title $title
118 * @param string $timestamp
119 * @param string $summary
120 * @param bool $minorEdit
121 * @param bool $oldid (default: false)
122 * @param string $pageStatus (default: 'changed')
124 public function notifyOnPageChange( $editor, $title, $timestamp, $summary,
125 $minorEdit, $oldid = false, $pageStatus = 'changed'
127 global $wgEnotifUseJobQ, $wgEnotifMinorEdits, $wgUsersNotifiedOnAllChanges, $wgEnotifUserTalk;
129 if ( $title->getNamespace() < 0 ) {
133 // update wl_notificationtimestamp for watchers
134 $watchers = self
::updateWatchlistTimestamp( $editor, $title, $timestamp );
137 // If nobody is watching the page, and there are no users notified on all changes
138 // don't bother creating a job/trying to send emails, unless it's a
139 // talk page with an applicable notification.
141 // $watchers deals with $wgEnotifWatchlist
142 if ( !count( $watchers ) && !count( $wgUsersNotifiedOnAllChanges ) ) {
144 // Only send notification for non minor edits, unless $wgEnotifMinorEdits
145 if ( !$minorEdit ||
( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
146 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK
);
147 if ( $wgEnotifUserTalk
149 && $this->canSendUserTalkEmail( $editor, $title, $minorEdit )
160 if ( $wgEnotifUseJobQ ) {
162 'editor' => $editor->getName(),
163 'editorID' => $editor->getID(),
164 'timestamp' => $timestamp,
165 'summary' => $summary,
166 'minorEdit' => $minorEdit,
168 'watchers' => $watchers,
169 'pageStatus' => $pageStatus
171 $job = new EnotifNotifyJob( $title, $params );
172 JobQueueGroup
::singleton()->lazyPush( $job );
174 $this->actuallyNotifyOnPageChange(
188 * Immediate version of notifyOnPageChange().
190 * Send emails corresponding to the user $editor editing the page $title.
192 * @note Do not call directly. Use notifyOnPageChange so that wl_notificationtimestamp is updated.
193 * @param User $editor
194 * @param Title $title
195 * @param string $timestamp Edit timestamp
196 * @param string $summary Edit summary
197 * @param bool $minorEdit
198 * @param int $oldid Revision ID
199 * @param array $watchers Array of user IDs
200 * @param string $pageStatus
201 * @throws MWException
203 public function actuallyNotifyOnPageChange( $editor, $title, $timestamp, $summary, $minorEdit,
204 $oldid, $watchers, $pageStatus = 'changed' ) {
205 # we use $wgPasswordSender as sender's address
206 global $wgEnotifWatchlist, $wgBlockDisablesLogin;
207 global $wgEnotifMinorEdits, $wgEnotifUserTalk;
209 # The following code is only run, if several conditions are met:
210 # 1. EmailNotification for pages (other than user_talk pages) must be enabled
211 # 2. minor edits (changes) are only regarded if the global flag indicates so
213 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK
);
215 $this->title
= $title;
216 $this->timestamp
= $timestamp;
217 $this->summary
= $summary;
218 $this->minorEdit
= $minorEdit;
219 $this->oldid
= $oldid;
220 $this->editor
= $editor;
221 $this->composed_common
= false;
222 $this->pageStatus
= $pageStatus;
224 $formattedPageStatus = array( 'deleted', 'created', 'moved', 'restored', 'changed' );
226 Hooks
::run( 'UpdateUserMailerFormattedPageStatus', array( &$formattedPageStatus ) );
227 if ( !in_array( $this->pageStatus
, $formattedPageStatus ) ) {
228 throw new MWException( 'Not a valid page status!' );
233 if ( !$minorEdit ||
( $wgEnotifMinorEdits && !$editor->isAllowed( 'nominornewtalk' ) ) ) {
234 if ( $wgEnotifUserTalk
236 && $this->canSendUserTalkEmail( $editor, $title, $minorEdit )
238 $targetUser = User
::newFromName( $title->getText() );
239 $this->compose( $targetUser );
240 $userTalkId = $targetUser->getId();
243 if ( $wgEnotifWatchlist ) {
244 // Send updates to watchers other than the current editor
245 // and don't send to watchers who are blocked and cannot login
246 $userArray = UserArray
::newFromIDs( $watchers );
247 foreach ( $userArray as $watchingUser ) {
248 if ( $watchingUser->getOption( 'enotifwatchlistpages' )
249 && ( !$minorEdit ||
$watchingUser->getOption( 'enotifminoredits' ) )
250 && $watchingUser->isEmailConfirmed()
251 && $watchingUser->getID() != $userTalkId
252 && !( $wgBlockDisablesLogin && $watchingUser->isBlocked() )
254 if ( Hooks
::run( 'SendWatchlistEmailNotification', array( $watchingUser, $title, $this ) ) ) {
255 $this->compose( $watchingUser );
262 global $wgUsersNotifiedOnAllChanges;
263 foreach ( $wgUsersNotifiedOnAllChanges as $name ) {
264 if ( $editor->getName() == $name ) {
265 // No point notifying the user that actually made the change!
268 $user = User
::newFromName( $name );
269 $this->compose( $user );
276 * @param User $editor
277 * @param Title $title
278 * @param bool $minorEdit
281 private function canSendUserTalkEmail( $editor, $title, $minorEdit ) {
282 global $wgEnotifUserTalk, $wgBlockDisablesLogin;
283 $isUserTalkPage = ( $title->getNamespace() == NS_USER_TALK
);
285 if ( $wgEnotifUserTalk && $isUserTalkPage ) {
286 $targetUser = User
::newFromName( $title->getText() );
288 if ( !$targetUser ||
$targetUser->isAnon() ) {
289 wfDebug( __METHOD__
. ": user talk page edited, but user does not exist\n" );
290 } elseif ( $targetUser->getId() == $editor->getId() ) {
291 wfDebug( __METHOD__
. ": user edited their own talk page, no notification sent\n" );
292 } elseif ( $wgBlockDisablesLogin && $targetUser->isBlocked() ) {
293 wfDebug( __METHOD__
. ": talk page owner is blocked and cannot login, no notification sent\n" );
294 } elseif ( $targetUser->getOption( 'enotifusertalkpages' )
295 && ( !$minorEdit ||
$targetUser->getOption( 'enotifminoredits' ) )
297 if ( !$targetUser->isEmailConfirmed() ) {
298 wfDebug( __METHOD__
. ": talk page owner doesn't have validated email\n" );
299 } elseif ( !Hooks
::run( 'AbortTalkPageEmailNotification', array( $targetUser, $title ) ) ) {
300 wfDebug( __METHOD__
. ": talk page update notification is aborted for this user\n" );
302 wfDebug( __METHOD__
. ": sending talk page update notification\n" );
306 wfDebug( __METHOD__
. ": talk page owner doesn't want notifications\n" );
313 * Generate the generic "this page has been changed" e-mail text.
315 private function composeCommonMailtext() {
316 global $wgPasswordSender, $wgNoReplyAddress;
317 global $wgEnotifFromEditor, $wgEnotifRevealEditorAddress;
318 global $wgEnotifImpersonal, $wgEnotifUseRealName;
320 $this->composed_common
= true;
322 # You as the WikiAdmin and Sysops can make use of plenty of
323 # named variables when composing your notification emails while
324 # simply editing the Meta pages
327 $postTransformKeys = array();
328 $pageTitleUrl = $this->title
->getCanonicalURL();
329 $pageTitle = $this->title
->getPrefixedText();
331 if ( $this->oldid
) {
332 // Always show a link to the diff which triggered the mail. See bug 32210.
333 $keys['$NEWPAGE'] = "\n\n" . wfMessage( 'enotif_lastdiff',
334 $this->title
->getCanonicalURL( array( 'diff' => 'next', 'oldid' => $this->oldid
) ) )
335 ->inContentLanguage()->text();
337 if ( !$wgEnotifImpersonal ) {
338 // For personal mail, also show a link to the diff of all changes
339 // since last visited.
340 $keys['$NEWPAGE'] .= "\n\n" . wfMessage( 'enotif_lastvisited',
341 $this->title
->getCanonicalURL( array( 'diff' => '0', 'oldid' => $this->oldid
) ) )
342 ->inContentLanguage()->text();
344 $keys['$OLDID'] = $this->oldid
;
345 // Deprecated since MediaWiki 1.21, not used by default. Kept for backwards-compatibility.
346 $keys['$CHANGEDORCREATED'] = wfMessage( 'changed' )->inContentLanguage()->text();
348 # clear $OLDID placeholder in the message template
349 $keys['$OLDID'] = '';
350 $keys['$NEWPAGE'] = '';
351 // Deprecated since MediaWiki 1.21, not used by default. Kept for backwards-compatibility.
352 $keys['$CHANGEDORCREATED'] = wfMessage( 'created' )->inContentLanguage()->text();
355 $keys['$PAGETITLE'] = $this->title
->getPrefixedText();
356 $keys['$PAGETITLE_URL'] = $this->title
->getCanonicalURL();
357 $keys['$PAGEMINOREDIT'] = $this->minorEdit ?
358 wfMessage( 'minoredit' )->inContentLanguage()->text() : '';
359 $keys['$UNWATCHURL'] = $this->title
->getCanonicalURL( 'action=unwatch' );
361 if ( $this->editor
->isAnon() ) {
362 # real anon (user:xxx.xxx.xxx.xxx)
363 $keys['$PAGEEDITOR'] = wfMessage( 'enotif_anon_editor', $this->editor
->getName() )
364 ->inContentLanguage()->text();
365 $keys['$PAGEEDITOR_EMAIL'] = wfMessage( 'noemailtitle' )->inContentLanguage()->text();
368 $keys['$PAGEEDITOR'] = $wgEnotifUseRealName && $this->editor
->getRealName() !== ''
369 ?
$this->editor
->getRealName() : $this->editor
->getName();
370 $emailPage = SpecialPage
::getSafeTitleFor( 'Emailuser', $this->editor
->getName() );
371 $keys['$PAGEEDITOR_EMAIL'] = $emailPage->getCanonicalURL();
374 $keys['$PAGEEDITOR_WIKI'] = $this->editor
->getUserPage()->getCanonicalURL();
375 $keys['$HELPPAGE'] = wfExpandUrl(
376 Skin
::makeInternalOrExternalUrl( wfMessage( 'helppage' )->inContentLanguage()->text() )
379 # Replace this after transforming the message, bug 35019
380 $postTransformKeys['$PAGESUMMARY'] = $this->summary
== '' ?
' - ' : $this->summary
;
382 // Now build message's subject and body
385 // enotif_subject_deleted, enotif_subject_created, enotif_subject_moved,
386 // enotif_subject_restored, enotif_subject_changed
387 $this->subject
= wfMessage( 'enotif_subject_' . $this->pageStatus
)->inContentLanguage()
388 ->params( $pageTitle, $keys['$PAGEEDITOR'] )->text();
391 // enotif_body_intro_deleted, enotif_body_intro_created, enotif_body_intro_moved,
392 // enotif_body_intro_restored, enotif_body_intro_changed
393 $keys['$PAGEINTRO'] = wfMessage( 'enotif_body_intro_' . $this->pageStatus
)
394 ->inContentLanguage()->params( $pageTitle, $keys['$PAGEEDITOR'], $pageTitleUrl )
397 $body = wfMessage( 'enotif_body' )->inContentLanguage()->plain();
398 $body = strtr( $body, $keys );
399 $body = MessageCache
::singleton()->transform( $body, false, null, $this->title
);
400 $this->body
= wordwrap( strtr( $body, $postTransformKeys ), 72 );
402 # Reveal the page editor's address as REPLY-TO address only if
403 # the user has not opted-out and the option is enabled at the
404 # global configuration level.
405 $adminAddress = new MailAddress( $wgPasswordSender,
406 wfMessage( 'emailsender' )->inContentLanguage()->text() );
407 if ( $wgEnotifRevealEditorAddress
408 && ( $this->editor
->getEmail() != '' )
409 && $this->editor
->getOption( 'enotifrevealaddr' )
411 $editorAddress = MailAddress
::newFromUser( $this->editor
);
412 if ( $wgEnotifFromEditor ) {
413 $this->from
= $editorAddress;
415 $this->from
= $adminAddress;
416 $this->replyto
= $editorAddress;
419 $this->from
= $adminAddress;
420 $this->replyto
= new MailAddress( $wgNoReplyAddress );
425 * Compose a mail to a given user and either queue it for sending, or send it now,
426 * depending on settings.
428 * Call sendMails() to send any mails that were queued.
431 function compose( $user ) {
432 global $wgEnotifImpersonal;
434 if ( !$this->composed_common
) {
435 $this->composeCommonMailtext();
438 if ( $wgEnotifImpersonal ) {
439 $this->mailTargets
[] = MailAddress
::newFromUser( $user );
441 $this->sendPersonalised( $user );
446 * Send any queued mails
448 function sendMails() {
449 global $wgEnotifImpersonal;
450 if ( $wgEnotifImpersonal ) {
451 $this->sendImpersonal( $this->mailTargets
);
456 * Does the per-user customizations to a notification e-mail (name,
457 * timestamp in proper timezone, etc) and sends it out.
458 * Returns true if the mail was sent successfully.
460 * @param User $watchingUser
464 function sendPersonalised( $watchingUser ) {
465 global $wgContLang, $wgEnotifUseRealName;
466 // From the PHP manual:
467 // Note: The to parameter cannot be an address in the form of
468 // "Something <someone@example.com>". The mail command will not parse
469 // this properly while talking with the MTA.
470 $to = MailAddress
::newFromUser( $watchingUser );
472 # $PAGEEDITDATE is the time and date of the page change
473 # expressed in terms of individual local time of the notification
474 # recipient, i.e. watching user
476 array( '$WATCHINGUSERNAME',
479 array( $wgEnotifUseRealName && $watchingUser->getRealName() !== ''
480 ?
$watchingUser->getRealName() : $watchingUser->getName(),
481 $wgContLang->userDate( $this->timestamp
, $watchingUser ),
482 $wgContLang->userTime( $this->timestamp
, $watchingUser ) ),
485 return UserMailer
::send( $to, $this->from
, $this->subject
, $body, array(
486 'replyTo' => $this->replyto
,
491 * Same as sendPersonalised but does impersonal mail suitable for bulk
492 * mailing. Takes an array of MailAddress objects.
493 * @param MailAddress[] $addresses
494 * @return Status|null
496 function sendImpersonal( $addresses ) {
499 if ( empty( $addresses ) ) {
504 array( '$WATCHINGUSERNAME',
507 array( wfMessage( 'enotif_impersonal_salutation' )->inContentLanguage()->text(),
508 $wgContLang->date( $this->timestamp
, false, false ),
509 $wgContLang->time( $this->timestamp
, false, false ) ),
512 return UserMailer
::send( $addresses, $this->from
, $this->subject
, $body, array(
513 'replyTo' => $this->replyto
,